<template>
  <van-icon
    :class="{ like: value === 1 }"
    :name="value ? 'good-job' : 'good-job-o'"
    @click="onLike"
    :loading="loading"
    loading-type="spinner"
  />
</template>

<script>
export default {
  name: 'likeArticle',
  data() {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String],
      required: true
    }
  },
  methods: {
    async onLike() {
      this.loading = true
      console.log(this.value)
      try {
        let status = -1
        if (this.value === 1) {
          // 取消点赞
          await this.$api.cancelLike(this.articleId)
        } else {
          // 点一个赞 👍
          await this.$api.giveLike(this.articleId)
          status = 1
        }
        // 提示信息
        this.$emit('input', status)
        this.$toast(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('操作失败,请稍后重试')
      }
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.like {
  color: red;
}
</style>
